package com.pinc.cpbl;

import android.content.Intent;
import android.os.Bundle;
import android.preference.CheckBoxPreference;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceChangeListener;
import android.preference.PreferenceActivity;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.kuad.KuBanner;
import com.kuad.kuADListener;

public class LiveSetting extends PreferenceActivity implements kuADListener{
	private Button b_left;
	private Button b_right;
	private TextView tv_title;
	protected LinearLayout mAdLayout;
	protected KuBanner mKuAdview;
	private CheckBoxPreference cbp_LiveBroadcast;
	private CheckBoxPreference cbp_isFavorite;
	private ListPreference lp_FavoiteTeam;
	private OnPreferenceChangeListener mPreferenceChangeListener= new OnPreferenceChangeListener(){
		@Override
		public boolean onPreferenceChange(Preference preference, Object newValue) {
			if (preference.getKey().compareTo(PreferenceHelper.FAVOR_TEAM) == 0)
			{
				lp_FavoiteTeam.setSummary((String)newValue);
			}
			new Thread(new Runnable(){
				@Override
				public void run() {
					try {
						Thread.sleep(500);
						startPhoneBroadcastService();
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
			}).start();
			return true;
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.live_setting);
		addPreferencesFromResource(R.xml.system_setting);

		tv_title = (TextView)findViewById(R.id.textView_title);
		b_left = (Button) findViewById(R.id.button_left);
		b_right = (Button) findViewById(R.id.button_right);

		tv_title.setText(R.string.title_setting);

		b_left.setVisibility(View.INVISIBLE);

		b_right.setText(getResources().getString(R.string.str_back));
		b_right.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});

		mAdLayout = (LinearLayout) this.findViewById(R.id.linearLayoutAD);
		mKuAdview = (KuBanner) this.findViewById(R.id.kuadview);
		mKuAdview.setkuADListener(this);

		cbp_LiveBroadcast = (CheckBoxPreference)this.findPreference("isLiveBroadcast");
		cbp_isFavorite = (CheckBoxPreference)this.findPreference("isFavorite");
		lp_FavoiteTeam = (ListPreference)this.findPreference("FavoriteTeam");

		cbp_LiveBroadcast.setOnPreferenceChangeListener(mPreferenceChangeListener);
		cbp_isFavorite.setOnPreferenceChangeListener(mPreferenceChangeListener);
		lp_FavoiteTeam.setOnPreferenceChangeListener(mPreferenceChangeListener);

		if ( lp_FavoiteTeam.getEntry() != null)
			lp_FavoiteTeam.setSummary(lp_FavoiteTeam.getEntry());
		else
			lp_FavoiteTeam.setSummary(getResources().getString(R.string.summary_choose_favorite_team));
	}

	@Override
	protected void onResume() {
		super.onResume();
		loadAD();
	}

	private void startPhoneBroadcastService()
	{
		Intent i = new Intent(getApplicationContext(), BroadcastService.class);
		startService(i);
	}

	private void loadAD()
	{
		mAdLayout.setVisibility(View.GONE);
		loadADwoGone();
	}

	private void loadADwoGone()
	{
		mKuAdview.setAPID("000000izO");
	}

	@Override
	public void onRecevie(String msg) {
		mAdLayout.setVisibility(View.VISIBLE);
	}

	@Override
	public void onFailedRecevie(String msg) {
	}
}
